package com.wisezone.permission.web;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.wisezone.common.annotation.WzWebAnnotation;
import com.wisezone.common.exception.WzWebException;
import com.wisezone.common.utils.BeanUtils;
import com.wisezone.common.utils.ConstantUtils;
import com.wisezone.common.web.BaseFormController;
import com.wisezone.permission.bean.entity.WzPersonEntity;
import com.wisezone.permission.bean.entity.WzUserEntity;
import com.wisezone.permission.bean.form.WzUserForm;
import com.wisezone.permission.bean.search.WzPersonSearch;
import com.wisezone.permission.bean.search.WzUserSearch;
import com.wisezone.permission.service.WzGroupService;
import com.wisezone.permission.service.WzPersonService;
import com.wisezone.permission.service.WzRoleService;
import com.wisezone.permission.service.WzUserService;

/**
 * 
 *用户; InnoDB free: 24576 kB
 * 
 * @author create by create date 2010-07-29 14:44:49 update date 2010-07-29
 *         14:44:49
 * 
 */
@WzWebAnnotation(moduleName = "user")
public class WzUserController extends
		BaseFormController<WzUserEntity, WzUserSearch, WzUserForm> {

	/**
	  *
	  *
	  *
	  */
	private WzUserService wzUserService;

	private String roleIds; // 角色ids

	private String roleNames; // 角色名称

	private String groupIds; // 用户组ids

	private String groupNames; // 用户组名称

	private WzPersonService wzPersonService;
	
	private WzRoleService wzRoleService;
	
	private WzGroupService wzGroupService;

	private List<WzPersonEntity> personList;

	/**
	 * 返回清空条件列表
	 * 
	 * @return
	 * @throws WzWebException
	 */
	public String toList() throws WzWebException {

		searchEntity = new WzUserSearch();
		pagination = this.wzUserService.listFormByPage(searchEntity);
		return LIST;
	}

	/**
	 * 获得列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws WzWebException {

		searchEntity = getSearchEntity();
		pagination = this.wzUserService.listFormByPage(searchEntity);
		return LIST;
	}

	/**
	 * 进入新增
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterNew() throws WzWebException {

		if (id != null) {
			form = this.wzUserService.findFormById(id);
			form.setId(null);
		}
		return NEW_EDIT;
	}

	/**
	 * 进入修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterEdit() throws WzWebException {

		form = this.wzUserService.findFormById(id);
		Map<String,String> map = this.wzGroupService.getMapByRoleId(id);
		if(map != null){
			groupIds = map.get("ids");
			groupNames = map.get("names");
		}
		map = this.wzRoleService.getMapByRoleId(id);
		if(map != null){
			roleIds = map.get("ids");
			roleNames = map.get("names");
		}
		return NEW_EDIT;
	}

	/**
	 * 进入查看
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterView() throws WzWebException {

		form = this.wzUserService.findFormById(id);
		Map<String,String> map = this.wzGroupService.getMapByRoleId(id);
		if(map != null){
			groupIds = map.get("ids");
			groupNames = map.get("names");
		}
		map = this.wzRoleService.getMapByRoleId(id);
		if(map != null){
			roleIds = map.get("ids");
			roleNames = map.get("names");
		}
		return VIEW;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws WzWebException {

		entity = getEntity();
		entity.setState(ConstantUtils.STATE_EXIST);
		this.wzUserService.saveOrUpdate(entity, roleIds, groupIds);
		return toList();
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws WzWebException {

		entity = getEntity();
		WzUserEntity oldEntity = this.wzUserService.findById(id);
		BeanUtils.copyBean(oldEntity, entity);
		this.wzUserService.saveOrUpdate(oldEntity, roleIds, groupIds);
		return toList();
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws WzWebException {

		Collection<Serializable> idList = getIdList();
		if (idList != null) {
			this.wzUserService.delete(idList);
		}
		return toList();
	}

	public void setWzUserService(WzUserService wzUserService) {
		this.wzUserService = wzUserService;
	}

	public void setWzPersonService(WzPersonService wzPersonService) {
		this.wzPersonService = wzPersonService;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public String getRoleNames() {
		return roleNames;
	}

	public void setRoleNames(String roleNames) {
		this.roleNames = roleNames;
	}

	public String getGroupIds() {
		return groupIds;
	}

	public void setGroupIds(String groupIds) {
		this.groupIds = groupIds;
	}

	public String getGroupNames() {
		return groupNames;
	}

	public void setGroupNames(String groupNames) {
		this.groupNames = groupNames;
	}

	public void setWzRoleService(WzRoleService wzRoleService) {
		this.wzRoleService = wzRoleService;
	}

	public void setWzGroupService(WzGroupService wzGroupService) {
		this.wzGroupService = wzGroupService;
	}

	public List<WzPersonEntity> getPersonList() {

		personList = wzPersonService.listAll(new WzPersonSearch());
		return personList;
	}

}